import { Component, Injector, ViewEncapsulation } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    PipelineListDto,
    BatchTaskServiceProxy,
    BatchDto,
    NewSideTaskInput,
    DepartmentServiceProxy,
    TaskDto,
    NameValueDtoOfInt32,
    PrintServiceProxy,
    ComboboxItemDto,
    PrintCommonDto,
    PrintInput,
    InputSideTaskDto,
    PrintOutList,
    JJDList,
    PrintOutDto,
    PipelineNodeServiceProxy,
    OutAssistListDto,
} from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import * as moment from 'moment';

@Component({
    selector: 'rw6',
    templateUrl: './rw6.component.html',
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()],
})
export class RW6ModalComponent extends AppComponentBase {
    saving = false;

    pipe: PipelineListDto = new PipelineListDto();
    batch = new BatchDto();
    teamGroups: ComboboxItemDto[] = [];
    teamGroupName: ComboboxItemDto;
    departments = new Array<NameValueDtoOfInt32>();
    departmentValue: number;
    printInput = new PrintCommonDto();
    receptionTime = new Date();
    sideProperty = '';
    oldprint = '';
    inputSideTaskDto = new InputSideTaskDto();
    jJDList = new Array<JJDList>();
    jJD = new JJDList();
    remark = '';
    outplan: OutAssistListDto;
    isHaveYQDate=false;

    teamGroupsB: ComboboxItemDto[] = [];
    teamGroupNameB: ComboboxItemDto;

    constructor(
        injector: Injector,
        private _departmentServiceProxy: DepartmentServiceProxy,
        private _taskServiceProxy: BatchTaskServiceProxy,
        private _printServiceProxy: PrintServiceProxy,
        private _pipelineNodesService: PipelineNodeServiceProxy
    ) {
        super(injector);
    }

    load(pipe: PipelineListDto): void {
        this.pipe = pipe;
        this._pipelineNodesService.getOutplant(pipe.id).subscribe((result) => {
            this.teamGroupsB = result.groups;
        });
        this._pipelineNodesService
        .getOutById(this.pipe.id)
        .subscribe((result) => {
            // this.teamGroupName = result.groupName;
            this.outplan = result;
          if(this.outplan.yqDate!=undefined){
            if(this.outplan.yqDate.toDate().getFullYear()!=1){
                this.receptionTime=this.outplan.yqDate.toDate()
                this.isHaveYQDate=true;
            }
          }
            
           
        });
        this._taskServiceProxy.getBatch(pipe.taskId).subscribe((result) => {
            this.batch = result;
            if (this.batch.deliveryRecord != undefined) {
                this.remark = this.batch.deliveryRecord.remark;
            }
            //console.log(result);
        });
        this._printServiceProxy.getPrintDrow().subscribe((result) => {
            // this.teamGroupName = result.groupName;
            this.teamGroups = result;
            this._printServiceProxy.getUserPrint().subscribe((result1) => {
                if (result1 != null) {
                    this.oldprint = result1.value;
                }
                this.teamGroupName = result1;
            });
        });
        
    }

    save(): void {
        this.saving = true;
        let receptionTime = moment(this.receptionTime).add(8, 'hours');
        this._pipelineNodesService
            .updateOutplan(this.pipe.id, receptionTime)
            .pipe(
                finalize(() => {
                    this.saving = false;
                })
            )
            .subscribe((result) => {
                this.notify.success('生成交接单成功');
                this._pipelineNodesService
                    .getOutById(this.pipe.id)
                    .subscribe((result) => {
                        this.outplan = result;
                        if(this.outplan.yqDate.toDate().getFullYear()!=1){
                            this.receptionTime=this.outplan.yqDate.toDate()
                            this.isHaveYQDate=true;
                        }
                    });
            });
    }

    print(): void {
        this.primengTableHelper.showLoadingIndicator();
        if (this.oldprint != this.teamGroupName.value) {
            this._printServiceProxy
                .createOrUpdateUserPrint(this.teamGroupName.value)
                .subscribe((result) => {});
        }
        this.printInput.printOutDto = new PrintOutDto();
        this.printInput.printSet = this.teamGroupName.value;
        this.printInput.type = 'WXOut';
        this.printInput.printOutDto.deptName = '51车间';
        this.printInput.printOutDto.receiveWorkShop = this.pipe.mainTeamGroup;
        this.printInput.printOutDto.wxjjdBarcode = 'WXJJD';

        var outList = new Array<PrintOutList>();
        var out = new PrintOutList();

        out.taskNum = this.pipe.taskCode;
        out.productName = this.pipe.taskName;
        out.drawingNum = this.pipe.drawingCode;
        out.finishDate = moment(this.receptionTime).add(8, 'hours');
        out.barcode = this.pipe.barCode;
        out.makeAmount = this.pipe.amountPlanned.toString();

        outList.push(out);
        this.printInput.printOutDto.outList = outList;

        this._printServiceProxy
            .printOut(this.printInput, this.pipe.id)
            .subscribe((result) => {
                this.notify.success('打印交接单成功');
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
}
